import CryptoJS from "crypto-js";
import config from '@/config'
/**
 * @description 处理图片地址
 * @param {String} url 图片链接
 * @param {String} thumbSize 缩略图片尺寸
 * @example  缩略图片 thumbSize值： 100x70
 * @returns
 */
export function handleImageUrl(url, thumbSize) {
  // 原图、缩略图图片地址前缀
  // const [domain, thumbDomain] = [
  //   "https://static-gateway.wasu.cn/upload",
  //   "https://static-gateway.wasu.cn/thumb",
  // ];
  const [domain, thumbDomain] = [
    config.baseUrl,
    config.baseUrl,
  ];
  const imgUrl = thumbSize
    ? `${thumbDomain}${url}?resize=${thumbSize}`
    : `${domain}${url}`;

  return imgUrl;
}

function authVideo(str) {
  const authKey = "nHAcrwnrbURHgX82SDCq";

  let _str = str.replace(/^(http:|https:)\/\/[^/]+/, "");
  const _time = parseInt(new Date().getTime() / 1000) + 7200;
  const uuid = newGuid();
  _str = _str + "-" + _time + "-" + uuid + "-0-" + authKey;
  let HashValue = CryptoJS.MD5(_str).toString();
  let authUrl = str + "?auth_key=" + _time + "-" + uuid + "-0-" + HashValue;
  return authUrl;
}
function newGuid() {
  let s4 = () => ((65536 * (1 + Math.random())) | 0).toString(16).substring(1);

  return (
    s4() +
    s4() +
    s4() +
    "4" +
    s4().substring(0, 3) +
    s4() +
    s4() +
    s4() +
    s4()
  ).toUpperCase();
}
/**
 * @description 处理视频防盗链
 * @param {string} str 视频播放链接
 * @returns string
 */
export function handleAuthVideoUrl(str) {
  str = str.replace("video.5g.wasu.tv", "eduvideo.5g.wasu.tv");
  let url = authVideo(str);
  return url;
}

/**
 * @description 终端区分
 * @returns string
 */
export const getENVIR = () => {
  const ua = navigator.userAgent.toLowerCase();
  const isWeixin = ua.indexOf("micromessenger") !== -1;
  const isInApp = /(^|;\s)app\//.test(ua);
  if (isWeixin) {
    if (window?.__wxjs_environment === "miniprogram") {
      return "wxapp";
    } else {
      return "wxh5";
    }
  } else {
    if (!isInApp) {
      return "browser";
    } else {
      return "app";
    }
  }
};

 /*判断客户端*/
export function judgeClient() {
  let u = navigator.userAgent;
  let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;   //判断是否是 android终端
  let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);     //判断是否是 iOS终端

  if(isAndroid){
    return 'Android';
  }else if(isIOS){
    return 'IOS';
  }else{
    return 'PC';
  }
}


/**
 * 显示消息提示框
 * @param content 提示的标题
 */
export function toast(content) {
  uni.showToast({
    icon: "none",
    title: content,
  });
}

/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
export function showConfirm(content) {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: "提示",
      content: content,
      cancelText: "取消",
      confirmText: "确定",
      success: function (res) {
        resolve(res);
      },
    });
  });
}

/**
 * 参数处理
 * @param params 参数
 */
export function tansParams(params) {
  let result = "";
  for (const propName of Object.keys(params)) {
    const value = params[propName];
    var part = encodeURIComponent(propName) + "=";
    if (value !== null && value !== "" && typeof value !== "undefined") {
      if (typeof value === "object") {
        for (const key of Object.keys(value)) {
          if (
            value[key] !== null &&
            value[key] !== "" &&
            typeof value[key] !== "undefined"
          ) {
            let params = propName + "[" + key + "]";
            var subPart = encodeURIComponent(params) + "=";
            result += subPart + encodeURIComponent(value[key]) + "&";
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&";
      }
    }
  }
  return result;
}
